package transaction.imp;

import affilication.Affiliation;
import classification.PaymentClassification;
import dao.PayrollDatabase;
import dao.imp.GpayrollDatabase;
import domain.Employee;
import domain.PayCheck;
import exception.EmployeeException;
import schedule.PaymentSchedule;
import transaction.Transaction;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PayDayTransaction implements Transaction {
    protected PayrollDatabase payrollDatabase = new GpayrollDatabase();
    private Date payDate;
    private Map<Integer, PayCheck> payCheckMap = new HashMap<>();

    public PayDayTransaction(Date payDate) {
        this.payDate = payDate;
    }

    @Override
    public void execute() throws EmployeeException {
        List<Employee> employees = payrollDatabase.getAllEmployees();
        for(Employee employee : employees) {
            PaymentSchedule schedule = employee.getSchedule();
            if(schedule.isPayDate(payDate)) {
                PaymentClassification classification = employee.getClassification();
                Affiliation affiliation = employee.getAffiliation();
                PayCheck payCheck = new PayCheck();

                double grossPay = classification.calcGrossPay();
                double deductions = affiliation == null ? 0 : affiliation.getDeduction();
                payCheck.setGrossPay(grossPay);
                payCheck.setDeductions(deductions);
                payCheck.setNetPay(grossPay - deductions);
                payCheck.setPayDate(payDate);
                payCheckMap.put(employee.getId(), payCheck);
            }
        }
    }

    public PayCheck getPaycheck(int empId) {
        return payCheckMap.get(empId);
    }
}
